<?php


require_once("fb_sdk/facebook.php");
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;

class Communicator extends Facebook{
	protected $accessToken;
	protected $userId;

	public function __construct(){		
		$config = array('appId' => APP_ID_, 'secret' => APP_SECRET_);		
				
		parent::__construct($config);
	}

	public function getUserId(){
		return parent::getUser();
	}

	public function getToken(){
		return parent::getAccessToken();
	}

	public function retrieveToken(){
		$this->accessToken = parent::getAccessToken();
	}

	public function setToken($accessToken){
		$this->accessToken = $accessToken;
	}

	public function setUserId($userId){
		$this->userId = $userId;
	}

	public function retrieveUser(){
		$this->userId = parent::getUser();
	}

	public function getData($id = 'me',$fields = null){
		$args = array();

		$args[] = 'access_token='.ACCESS_TOKEN_;

		if($fields != null){
			if(is_array($fields)){
				$fieldsList = array();
				foreach($fields as $key => $value){
					if(is_string($key)){
						$fieldsList[] = $key.'.fields('.implode(',',$value).')';
					}else{
						$fieldsList[] = $value;
					}
				}

				$fields = implode(',',$fieldsList);
			}
			
			$args[] = 'fields='.$fields;
		}
		
		if(count($args) > 0) $args = '?'.implode('&', $args);

		$result = parent::api("/$id/$args");
		
		return $result;
	}

	public function getLoginUrl($permission = array('scope'=>'user_groups')){		
		return parent::getLoginUrl($permission);
	}

	public function getLogoutUrl($params = null){
		if($params == null) $params = array('next' => PUBLIC_ROOT_);

		return parent::getLogoutUrl($params);
	}

	public function sendNotification($user, $text = 'Você tem uma notificação deste aplicativo'){
		if($href == null) $href = PUBLIC_ROOT_;

		$data = array(
			'href'=> $href,
		    'access_token'=> ACCESS_TOKEN_,
		    'template'=> $text
		);

		try{
			$sendNotification = parent::api("/$user/notifications","POST",$data);
		}catch(FacebookApiException $e){}
	}

}

?>